#include"NewtonPoly.h"
#include<fstream>
#include<string>

int main(){
    /*建立插值点数组以及对应函数值数组*/
    vector<double> time = {0, 0, 3, 3, 5, 5, 8, 8, 13, 13};
    vector<double> dist_and_v = {0, 75, 225, 77, 383, 80, 623, 74, 993, 72};

    /*输出t=10s的位置和速度*/
    cout <<"当时间t=10s时:\n";
    cout << "车的位移s=" << Newton_poly_at_x(time, dist_and_v, 10) << "ft,\n";
    cout << "车的速度v=" << Newton_dif_at_x(time, dist_and_v, 10) << "ft/s.\n";

    /*将区间[0,13]给260等分,取样本点,导出坐标(t, v)到文件`ProblemD_data/file.txt`*/
    string filename = "ProblemD_data/file.txt"; //创建文件名  
    ofstream outputFile(filename);  //创建文件流对象
    if(outputFile.is_open()){
        for(int i = 0; i <= 260; i++){
            double x = i / 20.0;//取样本点
            double y = Newton_dif_at_x(time, dist_and_v, x);
                                //取插值多项式在该样本点处的导数值
            outputFile << x << " " << y << endl;//导出到txt文件中
        }
        outputFile.close(); //关闭文件  
        cout << "File " << filename << " created and written successfully.\n";
    }
    else{  
        cout << "Error creating file: " << filename << endl;  
    }

    return 0;
}